<?php
namespace app\common\controller;

use think\Controller;
use think\Session;
use think\Db;

/**
* 
*/
class Base extends Controller
{
	public $controller;
    public $action;
    public $dataError = "";//错误数据提交
    public $configArr = [];
    public $levelName = [];



    public function _initialize() {

        parent::_initialize();

        $this->controller = request()->controller();
        $this->action = request()->action();
        //等级名
        /*$nameArr = Db::name("team")->field("level,name")->select();
        $this->levelName = return_key_val($nameArr,"level","name");
        $this->levelName[0] = "普通会员";*/

        //读取配置
        $Arr = Db::name("config")->field("key,value")->select();
        $this->configArr = return_key_val($Arr,"key","value");
        $this->assign("configArr",$this->configArr);

        $is_weixin = is_weixin();
        $this->assign("is_weixin",$is_weixin);

        if( Session::has("userInfo") ){
            $user_info = Session::get("userInfo");
            $this->userInfo = $user_info;
            if( !empty($user_info) ){
                $userArr = app_model("index","user")->getuserInfo($this->userInfo["id"],"a.nickname,a.account,a.portrait,a.add_time,b.*,l.id as level_id,l.level");
                $userArr["end_time"] = date("m/d/Y H:i:s",$userArr["add_time"]+60*60*24*27); 
            }
            
        }else{
            $userArr = array();
        }
        $this->assign("userArr",$userArr);

        $logo = Db::name('config')->where('key','web_logo')->value('value');
        $this->assign("logo",$logo);
        
    }

    public function json_return($data,$files="dataError"){

        if( !empty($this->dataError) ){
            $str = $this->controller."|".$this->action."|".$files."=".$this->dataError;
            create_api_log($str,$files);
        }
        echo json_encode($data);
        exit();
    }

    public function data_verify($data,$filed){
        $ret = true;
        if(empty($data) || empty($filed) ) return false;

        $Arr = explode(",",$filed);
        $Arr = array_filter($Arr);
        foreach($Arr as $k=>$v){
            if( !isset($data[$v]) || empty($data[$v]) ){
                $ret = false;
                break;
            }
        }
        return $ret;
    }

}